package com.action;

import java.util.ArrayList;
import java.util.List;

import com.cms.model.Channel;
import com.cms.service.ChannelServiceImpl;
import com.model.Admin;
import com.model.AdminChannel;
import com.model.Role;
import com.service.AdminChannelService;
import com.service.AdminChannelServiceImpl;
import com.service.AdminService;
import com.service.AdminServiceImpl;
import com.service.RoleService;
import com.service.RoleServiceImpl;


/**   
 *   
 * @author zhanghusong   
 */
public class AdminAction extends DefaultAction {

	private static final long serialVersionUID = 1L;
	AdminService service;
	List<Role> roleList;
	Admin model;
	List<Admin> list;
	RoleService rservice;
	String[] spArray;
	String[] chArray;
	List<Channel> chlist;
	String chstr="";
	
	public AdminAction()
	{
		service = new AdminServiceImpl();
		rservice = new RoleServiceImpl();
	}
	String type="";
	String msg="";
	String txt_keyword = "";
	public String list()
	{ 
		if(txt_keyword==null || "".equals(txt_keyword.trim()))
			list = service.getAdmin();
		else
			list = service.getAdminByKeyWord(txt_keyword);
		roleList = rservice.getRole();
		return SUCCESS;
	}
	public String save()
	{ 
		if(type.equals("add"))
		{ 
			model.setCreateUser(getAdminLoginName());
			int adminid = service.saveAdmin(model);
			if(adminid>0){
				msg = "用户管理:添加成功！";
				String[] chstrs = getRequest().getParameterValues("channel_checkbox");
				AdminChannelService adchservice = new AdminChannelServiceImpl();
				List<AdminChannel> adchlist = new ArrayList<AdminChannel>();
				if(chstrs!=null){
					for (int i = 0; i < chstrs.length; i++) {
						AdminChannel adch = new AdminChannel();
						adch.setAdminid(adminid);
						adch.setChannelid(Integer.parseInt(chstrs[i]));
						adchlist.add(adch);
					}
				}
				adchservice.saveAdminChannel(adchlist);
				
			}else{
				msg = "用户管理:添加失败！请重新添加！";
			}
		}
		else if(type.equals("update"))
		{
			if(service.updateAdmin(model)){
				msg = "用户管理:更新成功！";
				String[] chstrs = getRequest().getParameterValues("channel_checkbox");
				AdminChannelService adchservice = new AdminChannelServiceImpl();
				List<AdminChannel> adchlist = new ArrayList<AdminChannel>();
				if(chstrs!=null){
					for (int i = 0; i < chstrs.length; i++) {
						AdminChannel adch = new AdminChannel();
						adch.setAdminid(Integer.parseInt(model.getId()));
						adch.setChannelid(Integer.parseInt(chstrs[i]));
						adchlist.add(adch);
					}
				}
				adchservice.updateAdminChannel(model.getId(),adchlist);
			}else{
				msg = "用户管理:更新失败！请重新更新！";
			}
			type = "upd";
		}
		else if(type.equals("del"))
		{
			if(service.deleteAdmin(model))
				msg = "用户管理:删除成功！";
			else
				msg = "用户管理:删除失败！请重新删除！";
		}
		return SUCCESS;
	}
	public String show()
	{ 
//		if(type.equals("add"))
//		{ 
//			insertSpCp(model);
//			model.setCreateUser(getAdminLoginName());
//			if(service.saveAdmin(model))
//				msg = "添加成功";
//			else
//				msg = "添加失败！！请重新添加！！";
//		}
//		else if(type.equals("update"))
//		{
//			insertSpCp(model);
//			if(service.updateAdmin(model))
//				msg = "更新成功！！";
//			else
//				msg = "更新失败！！请重新更新！！";
//			type = "upd";
//		}
//		else if(type.equals("del"))
//		{
//			if(service.deleteAdmin(model))
//				msg = "删除成功！！";
//			else
//				msg = "删除失败！！请重新删除！！";
//		}
//		else 
		chlist = new ChannelServiceImpl().getChannels();
		if(type.equals("upd"))
		{
			model = service.getAdminById(model);
			spArray = model.getSp().split(",");
			chArray = model.getChannel().split(",");
			chstr=",";
			List<AdminChannel> adchlist = new AdminChannelServiceImpl().getlist(new AdminChannel(0,Integer.parseInt(model.getId()),0));
			for (Channel ch : chlist) {
				for (AdminChannel adminChannel : adchlist) {
					if(ch.getId()==adminChannel.getChannelid()){
						chstr+=ch.getId()+",";
					}
				}
			}
		}
//		else
//		{
//			model= new Admin();
//			model.setIsAdmin("false");
//		}
		//list = service.getAdmin();
		roleList = rservice.getRole();
		
		
		return SUCCESS;
	}
	
	public List<Role> getRoleList() {
		return roleList;
	}
	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
	public Admin getModel() {
		return model;
	}
	public void setModel(Admin model) {
		this.model = model;
	}
	public List<Admin> getList() {
		return list;
	}
	public void setList(List<Admin> list) {
		this.list = list;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public List<Channel> getChlist() {
		return chlist;
	}
	public void setChlist(List<Channel> chlist) {
		this.chlist = chlist;
	}
	public String getChstr() {
		return chstr;
	}
	public void setChstr(String chstr) {
		this.chstr = chstr;
	}

	
}
